package yxy.game.pm2.module.shop;

import org.joda.time.DateTime;
import org.joda.time.LocalDate;
import org.joda.time.Seconds;
import yxy.apple.util.redisson.RedisException;
import yxy.cherry.data.DataCenter;
import yxy.cherry.data.bean.magicshop;
import yxy.cherry.data.bean.shop;
import yxy.game.pm2.bean.Currency;
import yxy.game.pm2.bean.db.dao.MySQLCacheDAO;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Map;

/** 商店 兵法商店 */
public class StrategyShopModule extends ShopModule {

	// bean===========================================================================
	private int freeTimes; // 免费刷新剩余次数
	private DateTime freeRefreshTime; // 上次使用免费刷新的时间
	private int resetTimes;// 付费刷新次数
	private DateTime refreshTime; // 上次付费刷新的时间

	public int getFreeTimes() {
		return freeTimes;
	}

	public void setFreeTimes(int freeTimes) {
		this.freeTimes = freeTimes;
	}

	public DateTime getFreeRefreshTime() {
		return freeRefreshTime;
	}

	public void setFreeRefreshTime(DateTime freeRefreshTime) {
		this.freeRefreshTime = freeRefreshTime;
	}

	public int getResetTimes() {
		return resetTimes;
	}

	public void setResetTimes(int resetTimes) {
		this.resetTimes = resetTimes;
	}

	public DateTime getRefreshTime() {
		return refreshTime;
	}

	public void setRefreshTime(DateTime refreshTime) {
		this.refreshTime = refreshTime;
	}

	// func===========================================================================

	/** 可以刷新 */
	public boolean canRefresh() {
		// 免费剩余次数>0 或 付费次数<上限
		return refreshTimesFree() > 0 || refreshTimes() < RefreshUpper();
	}

	public long refreshTimesFreeTime() {
		if (getFreeTimes() >= RefreshFreeUpper()) {
			return 0;
		}
		DateTime now = DateTime.now();
		long lessTime = Seconds.secondsBetween(now, getFreeRefreshTime()).toStandardDuration().getMillis();
		return lessTime;
	}

	public int refreshTimesFree() {
		if (getFreeTimes() >= RefreshFreeUpper()) {
			return getFreeTimes();
		}
		DateTime now = DateTime.now();
		int passTime = Seconds.secondsBetween(getFreeRefreshTime(), now).getSeconds();
		while ((getFreeRefreshTime().isBefore(now) || passTime >= RefreshFreeTime().getSeconds()) && getFreeTimes() < RefreshFreeUpper()) {
			setFreeRefreshTime(getFreeRefreshTime().plusSeconds(RefreshFreeTime().getSeconds()));
			passTime = Seconds.secondsBetween(getFreeRefreshTime(), now).getSeconds();
			setFreeTimes(getFreeTimes() + 1);
		}
		this.set();
		return getFreeTimes();
	}

	public boolean refreshFree() {
		int times = refreshTimesFree();
		if (times <= 0) {
			return false;
		}
		DateTime now = DateTime.now();
		setFreeRefreshTime(now.plus(RefreshFreeTime()));
		setFreeTimes(times - 1);
		_refresh();
		return true;
	}

	public int refreshTimes() {
		DateTime now = DateTime.now();
		if (!new LocalDate(now).equals(new LocalDate(getRefreshTime()))) {
			setRefreshTime(now);
			setResetTimes(0);
			this.set();
		}
		return getResetTimes();
	}

	public void refresh() {
		setRefreshTime(DateTime.now());
		setResetTimes(getResetTimes() + 1);
		_refresh();
	}

	private void _refresh() {
		shuffleGoods();
		set();
	}

	// static===========================================================================

	static private shop data = data(109);

	/** 商店上限 */
	public static int ShopUpper() {
		return data.getShopmax();
	}

	/** 商店刷新消费 */
	public static Currency ShopRefreshCost() {
		return Currency.create(data.getRecosttype(), data.getRecostid(), data.getRecostnum());
	}

	/** 付费刷新次数上限 */
	public static int RefreshUpper() {
		return data.getRenum();
	}

	/** 免费刷新次数上限 */
	public static int RefreshFreeUpper() {
		return data.getRefreenum();
	}

	/** 兵法商店免费刷新增长时间(秒) */
	public static Seconds RefreshFreeTime() {
		return Seconds.seconds(data.getRefreetime());
	}

	public static String key(String playerCode) {
		return String.format(key(), playerCode, "STRA");
	}

	public static StrategyShopModule get(String playerCode) {
		try {
			StrategyShopModule module = getv(key(playerCode));
			if (module == null) {
				module = MySQLCacheDAO.getV(key(playerCode), StrategyShopModule.class);
				if(module != null){module.set();}//把数据同步到redis
			}

			String md5 = DataCenter.getMD5(magicshop.class);
			if (module == null) {
				module = create(playerCode, md5);
			} else {
				module.checkMD5(md5);
			}
			return module;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	private static StrategyShopModule create(String playerCode, String md5) {
		StrategyShopModule module = new StrategyShopModule();
		module.setPlayerCode(playerCode);
		module.setDataMD5(md5);
		DateTime now = DateTime.now();
		module.setFreeRefreshTime(now);
		module.setRefreshTime(now);
		module.setFreeTimes(RefreshFreeUpper());
		module.shuffleGoods();
		module.set();
		return module;
	}

	// extends===========================================================================

	@Override
	protected void shuffleGoods() {
		Map<String, magicshop> datas = DataCenter.getAllData(magicshop.class);
		ArrayList<magicshop> list = new ArrayList<>(datas.values());
		Collections.shuffle(list);

		DateTime now = DateTime.now();
		ArrayList<Goods> goodslist = new ArrayList<>();
		for (magicshop data : list) {
			Goods goods = new Goods();
			goods.setId(data.getId());
			goods.setDataId(data.getId());
			goods.setSell(0);
			goods.setUpdateTime(now);
			goodslist.add(goods);
			if (goodslist.size() >= ShopUpper()) {
				break;
			}
		}
		setGoods(goodslist);
	}

	//存储到数据库的方法
	public static void saveToDB(String playerCode) {
		try {
			String key = key(playerCode);
			if (touch(key).isExists()) {
				StrategyShopModule strategyShopModule = getv(key(playerCode));
				if(strategyShopModule != null)MySQLCacheDAO.setV(key, strategyShopModule);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	//从redis移走数据的操作
	public static void removeAllFromRedis(String playerCode) {
		try {
			touch(key(playerCode)).delete();
		} catch (RedisException e) {
			e.printStackTrace();
		}
	}

	@Override
	public void set() {
		try {
			setv(key(getPlayerCode()), this);
		} catch (RedisException e) {
		}
	}

}